package com.crazymaker.demo.drinkTea;

import lombok.extern.slf4j.Slf4j;
import rx.Observable;
import rx.functions.Func0;

@Slf4j
public class WashCupJob
{

    private boolean cupOk;

    public void doWash() throws InterruptedException
    {
        log.info("洗茶壶" );
        log.info("洗茶杯" );
        log.info("拿茶叶" );
        //线程睡眠一段时间，代表清洗中
        Thread.sleep(500);
        log.info("洗完了" );

        log.info(" 清洗工作  运行结束." );
        cupOk = true;

    }

    public Observable<Boolean> observable()
    {
        //        return Observable.defer(waterOk);
        return Observable.defer(new Func0<Observable<Boolean>>()
        {
            public Observable<Boolean> call()
            {

                try
                {
                    doWash();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();

                    log.info(" 清洗工作 发生异常被中断." );
                    cupOk = false;
                    return Observable.error(e);
                }

                return Observable.just(cupOk);

            }
        });

    }
}
